package com.yijiu.trb;

import java.util.Calendar;

import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.yijiu.action.BaseAction;
import com.yijiu.adapter.BrokerageListAdapter;
import com.yijiu.adapter.ScrollFootListener;
import com.yijiu.network.Request;
import com.yijiu.resource.ActionType;
import com.yijiu.resource.BrokerageList;
import com.yijiu.resource.C;
import com.yijiu.resource.CacheData;
import com.yijiu.response.RequestBrokerageRecordResponse;

/**
 * 佣金管理
 * 
 * @author TangChao
 */
public class MoneyManagerActivity extends BTitleActivity implements ScrollFootListener, OnItemClickListener
{
	/**
	 * 已收款佣金
	 */
	private TextView moneyShow0;

	/**
	 * 累计佣金
	 */
	private TextView moneyShow1;

	/**
	 * 列表
	 */
	private ListView listView;

	/**
	 * 适配器
	 */
	private BrokerageListAdapter brokerageAdapter;

	private int pageIndex = 1;

	private boolean isLoadOver, onFoot = false;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		getWindow().setFormat(PixelFormat.RGBA_8888);
		setContentView(R.layout.money_manager_layout);
		initWidget();
		RequestBrokerageRecord(pageIndex, onFoot);
	}

	@Override
	protected void initWidget()
	{
		super.initWidget();
		titleView.setText("佣金记录");
		moneyShow0 = (TextView) findViewById(R.id.moneyShow0);
		moneyShow1 = (TextView) findViewById(R.id.moneyShow1);
		listView = (ListView) findViewById(R.id.brokerage_listView);
		listView.setOnItemClickListener(this);
	}

	private String getNowDate(boolean isAddOne)
	{
		int year, month, day;
		String monthStr, dayStr;
		Calendar cur = Calendar.getInstance();
		if (isAddOne)
			cur.add(Calendar.DAY_OF_MONTH, 1);
		year = cur.get(Calendar.YEAR);
		month = cur.get(Calendar.MONTH) + 1;
		day = cur.get(Calendar.DAY_OF_MONTH);
		monthStr = (month < 10) ? ("0" + month) : ("" + month);
		dayStr = (day < 10) ? ("0" + day) : ("" + day);
		return year + "-" + monthStr + "-" + dayStr;
	}

	/**
	 * 佣金记录请求
	 */
	private void RequestBrokerageRecord(int pageIndex, boolean onFoot)
	{
		// 将用户输入的信息拼接成字符串发送到服务器
		String inputStr = "service=appBrokerageRecordService&pageNo=" + pageIndex + "&pageSize=10&startDate=2013-11-25&endDate="
				+ getNowDate(true);
		// 用私钥进行AES加密
		encryptBase(inputStr, C.PRIVATE_KEY);
		// 将用公钥加密后的私钥、用私钥加密后的数据进行封装
		BaseAction action = new BaseAction(encodedData, encriptData, CacheData.APP_KEY);
		RequestBrokerageRecordResponse requestBrokerageRecordResponse = new RequestBrokerageRecordResponse();
		// 请求服务器
		Request request = new Request(action, requestBrokerageRecordResponse, ActionType.REQUEST_BROKERAGE_RECORD);
		netManager.excute(request, this, this);
		if (onFoot)
		{
			brokerageAdapter.showprogress();
		}
		else
		{
			showProgress("正在获取佣金记录数据，请稍候");
		}
	}

	/**
	 * 请求成功返回的函数
	 */
	@Override
	public void getResult(Request r)
	{
		super.getResult(r);
		switch (r.getType())
		{
			case ActionType.REQUEST_BROKERAGE_RECORD:
				if (onFoot)
				{
					brokerageAdapter.hideprogress();
				}
				RequestBrokerageRecordResponse requestBrokerageRecordResponse = (RequestBrokerageRecordResponse) r.getResponse();
				if (requestBrokerageRecordResponse.list == null || requestBrokerageRecordResponse.list.isEmpty()
						|| requestBrokerageRecordResponse.list.size() < 10)
				{
					isLoadOver = true;
				}
				else
				{
					isLoadOver = false;
				}
				if (brokerageAdapter == null || pageIndex == 1)
				{
					brokerageAdapter = new BrokerageListAdapter(requestBrokerageRecordResponse.list, MoneyManagerActivity.this);
					brokerageAdapter.setScrollFootListener(this);
					listView.setAdapter(brokerageAdapter);
					moneyShow0.setText("￥" + requestBrokerageRecordResponse.payedAmount);
					moneyShow1.setText("￥" + requestBrokerageRecordResponse.allAmount);
				}
				else
				{
					if (requestBrokerageRecordResponse.list != null && !requestBrokerageRecordResponse.list.isEmpty())
					{
						brokerageAdapter.addAll(requestBrokerageRecordResponse.list);
					}
				}
				break;
			default:
				break;
		}
	}

	@Override
	public void notifyError(int type, int errorCode, String errorMsg)
	{
		super.notifyError(type, errorCode, errorMsg);
		app.showMsg(errorMsg);
	}

	@Override
	public void rePost(Request r)
	{
		super.rePost(r);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
	{
		BrokerageList item = (BrokerageList) arg0.getItemAtPosition(arg2);
		Log.i("tradeId", item.tradeId + "");
		Log.i("detailId", item.detailId + "");
		Log.i("investDetailId", item.investDetailId + "");
		Intent intent = new Intent(MoneyManagerActivity.this, BrokerageDetailActivity.class);
		intent.putExtra("tradeId", item.tradeId);
		intent.putExtra("detailId", item.detailId);
		intent.putExtra("investDetailId", item.investDetailId);
		intent.putExtra("investorName", item.investorName);
		startActivity(intent);
	}

	@Override
	public void onFoot()
	{
		if (isLoadOver)
		{
			return;
		}
		onFoot = true;
		RequestBrokerageRecord(++pageIndex, onFoot);
	}
}
